package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.DictMapper;
import com.stylefeng.guns.common.persistence.dao.InitialOrderMapper;
import com.stylefeng.guns.common.persistence.dao.TTrainMapper;
import com.stylefeng.guns.common.persistence.dto.ExportOrder;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.execl.EasyExcel;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.service.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wengys123
 * @since 2018-01-28
 */
@Service
public class InitialOrderServiceImpl extends ServiceImpl<InitialOrderMapper, InitialOrder> implements IInitialOrderService {
    @Resource
    private GunsProperties gunsProperties;
    @Resource
    private InitialOrderMapper initialOrderMapper;
    @Autowired
    private IRegionService iRegionService;
    @Autowired
    private ITOrderService itOrderService;
    @Autowired
    private IDeliveryRangeService iDeliveryRangeService;
    @Autowired
    private ITCooperationDotService itCooperationDotService;
    @Autowired
    private TTrainMapper tTrainMapper;

    @Autowired
    private DictMapper tDictMapper;

    @Override
    public List<Map<String, Object>> selectInitialOrder(Page<Map<String, Object>> page, Map m) {
        return initialOrderMapper.selectInitialOrder(page, m);
    }

    @Override
    public boolean export(String filepath, String returnpath, String batch_no, String is_normal) {
        Wrapper<InitialOrder> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("batch_no={0}", batch_no);
        if (is_normal != null && !is_normal.isEmpty()) {
            wrapper.andNew("is_normal={0}", is_normal);
        }
        List<InitialOrder> initialOrderList = initialOrderMapper.selectList(wrapper);
        try {
            File file = new File(filepath);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    throw new IOException("文件创建失败");
                }
            }
            //创建HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook wb = new HSSFWorkbook();
            //建立新的sheet对象（excel的表单）
            HSSFSheet sheet = wb.createSheet("Sheet1");
            //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
            HSSFRow row1 = sheet.createRow(0);
            //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
            HSSFCell cell = row1.createCell(0);
            //设置单元格内容
            //     cell.setCellValue(" );
            //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
            //     sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
            //在sheet里创建第二行
            HSSFRow row2 = sheet.createRow(0);
            //创建单元格并设置单元格内容
            row2.createCell(0).setCellValue("运单号");
            row2.createCell(1).setCellValue("发货人");
            row2.createCell(2).setCellValue("品名");
            row2.createCell(3).setCellValue("件数");
            row2.createCell(4).setCellValue("重量");
            row2.createCell(5).setCellValue("立方");
            row2.createCell(6).setCellValue("提付");
            row2.createCell(7).setCellValue("代收货款");
            row2.createCell(8).setCellValue("垫付款");
            row2.createCell(9).setCellValue("返款结算(填写'待结算'或'已清账')");
            row2.createCell(10).setCellValue("收货人");
            row2.createCell(11).setCellValue("收货电话");
            row2.createCell(12).setCellValue("交接方式(填写'配送'或'自提')");
            row2.createCell(13).setCellValue("等通知发货(填写'是'或'否')");
            row2.createCell(14).setCellValue("回单要求");
            row2.createCell(15).setCellValue("收货省份");
            row2.createCell(16).setCellValue("收货地市");
            row2.createCell(17).setCellValue("收货区县");
            row2.createCell(18).setCellValue("收货详细地址");
            row2.createCell(19).setCellValue("错误信息");

            for (int i = 0; i < initialOrderList.size(); i++) {
                HSSFRow row3 = sheet.createRow(i + 1);
                row3.createCell(0).setCellValue(initialOrderList.get(i).getOriginal_num());
                row3.createCell(1).setCellValue(initialOrderList.get(i).getSendDeptName());
                row3.createCell(2).setCellValue(initialOrderList.get(i).getGoodsName());
                row3.createCell(3).setCellValue(initialOrderList.get(i).getGoodsPiece());
                row3.createCell(4).setCellValue(initialOrderList.get(i).getGoodsWeight());
                row3.createCell(5).setCellValue(initialOrderList.get(i).getGoodsVolume());
                row3.createCell(6).setCellValue(initialOrderList.get(i).getPayment());
                row3.createCell(7).setCellValue(initialOrderList.get(i).getCollection());
                row3.createCell(8).setCellValue(initialOrderList.get(i).getBackAmount());
                row3.createCell(9).setCellValue(initialOrderList.get(i).getSettlement());
                row3.createCell(10).setCellValue(initialOrderList.get(i).getConsignee());
                row3.createCell(11).setCellValue(initialOrderList.get(i).getReceivingPhone());
                row3.createCell(12).setCellValue(initialOrderList.get(i).getMode());
                row3.createCell(13).setCellValue(initialOrderList.get(i).getNotice());
                row3.createCell(14).setCellValue(initialOrderList.get(i).getReceipt());
                row3.createCell(15).setCellValue(initialOrderList.get(i).getProvince());
                row3.createCell(16).setCellValue(initialOrderList.get(i).getCity());
                row3.createCell(17).setCellValue(initialOrderList.get(i).getArea());
                row3.createCell(18).setCellValue(initialOrderList.get(i).getReceivingAddress());
                row3.createCell(19).setCellValue(initialOrderList.get(i).getResult());
            }
            FileOutputStream fout = new FileOutputStream(filepath);
            wb.write(fout);
            fout.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    @Transactional
    public Map<String, Object> uploadExecl(MultipartFile multipartFile, String car_no, String driver_name, String arrive_date,String phone, String driverId,String transferId) {
        String filename = multipartFile.getOriginalFilename();
        String prefix = filename.substring(filename.lastIndexOf(".") + 1);
        String pictureName = UUID.randomUUID().toString() + "." + prefix;
        TCooperationDot tCooperationDot= itCooperationDotService.selectById(transferId);
        try {
            String fileSavePath = gunsProperties.getFileUploadPath();
            File file = new File(fileSavePath + pictureName);
            multipartFile.transferTo(file);
            EasyExcel fastExcel = new EasyExcel(file.getPath());
            fastExcel.setSheetName("Sheet1");
            List<ExportOrder> exportOrders = fastExcel.parse(ExportOrder.class);//1表格数据导入到list中
            List<InitialOrder> initOrders = new ArrayList<InitialOrder>();
            for (ExportOrder export : exportOrders) {
                InitialOrder initialOrder = new InitialOrder();
                BeanUtils.copyProperties(export, initialOrder);
                initialOrder.setVehicle_no(car_no);
                initialOrder.setDriver_name(driver_name);
                initialOrder.setArrive_date(DateUtil.parseDate(arrive_date));
                initialOrder.setAttachment_name(filename);
                initialOrder.setDriver_id(driverId);
                initialOrder.setSendDeptName(tCooperationDot.getDotName());
                initOrders.add(initialOrder);
            }
            //2 将表格中导入的数据转储到 List<InitialOrder>（导入的数据不一定与InitialOrder字段一致相同，因此导入的数据先放在ExportOrder对象中）

//            String bath_no = ShiroKit.getUser().getId() + DateUtil.getAllTimes();
            Map param=new HashMap();
            param.put("dot_id",ShiroKit.getUser().getDotId());
            param.put("nType", Const.NTYPE_00); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
            String bath_no = tDictMapper.getNumCode(param);
            List<String> ordernums=new ArrayList<>();

            // 循环行Row
            for (int i = 0; i < initOrders.size(); i++) {
                String result = "";//数据问题描述
                int normal = 1;//默认的数据状态
                try {
                    if (initOrders.get(i).getOriginal_num() == null || initOrders.get(i).getOriginal_num().isEmpty() || "null".equals(initOrders.get(i).getOriginal_num())) {
                        normal = 0;
                        result += "订单编号必填,";
                    } else {
                        Wrapper<TOrder> wrapper = new EntityWrapper<>();
                        wrapper = wrapper.where("original_num={0}", initOrders.get(i).getOriginal_num());
                        List<TOrder> initialOrderList = itOrderService.selectList(wrapper);
                        if (initialOrderList.size() > 0||ordernums.contains(initOrders.get(i).getOriginal_num())) {
                            normal = 0;
                            result += "订单编号重复,";
                        }else{
                            ordernums.add( initOrders.get(i).getOriginal_num());
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "订单编号不规范,";
                }
                try {
                    if (initOrders.get(i).getSendDeptName() == null || initOrders.get(i).getSendDeptName().isEmpty() || "null".equals(initOrders.get(i).getSendDeptName())) {
                        normal = 0;
                        result += "发货方名称不能为空,";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "发货方名称不规范,";
                }
                ;
//                try {
//                    if (i != 0) {
//                        if (!sendUser.equals(initOrders.get(i).getSendDeptName())) {
//                            normal = 0;
//                            result += "发货方不一致,";
//                        }
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    normal = 0;
//                    result += "发货方不一致,";
//                }

                try {
                    if (initOrders.get(i).getGoodsName() == null || initOrders.get(i).getGoodsName().isEmpty() || "null".equals(initOrders.get(i).getGoodsName().isEmpty())) {
                        normal = 0;
                        result += "物品名称不能为空,";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "物品名称不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getGoodsPiece() == null || initOrders.get(i).getGoodsPiece().isEmpty() || "null".equals(initOrders.get(i).getGoodsPiece())) {
                        normal = 0;
                        result += "物品件数不能为空,";
                    } else if (!initOrders.get(i).getGoodsPiece().split("\\.")[0].matches("[0-9]+")) {
                        normal = 0;
                        result += "物品件数只能填数字,";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "物品件数不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getGoodsWeight() != null && !initOrders.get(i).getGoodsWeight().isEmpty() && !"null".equals(initOrders.get(i).getGoodsWeight())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getGoodsWeight());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "物品重量不正确,";
                }
                try {
                    if (initOrders.get(i).getGoodsVolume() != null && !initOrders.get(i).getGoodsVolume().isEmpty() && !"null".equals(initOrders.get(i).getGoodsVolume())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getGoodsVolume());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "物品体积不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getPayment() != null && !initOrders.get(i).getPayment().isEmpty() && !"null".equals(initOrders.get(i).getPayment())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getPayment());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "提付不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getExtra_money() != null && !initOrders.get(i).getExtra_money().isEmpty() && !"null".equals(initOrders.get(i).getExtra_money())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getExtra_money());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "落地费不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getScene_money() != null && !initOrders.get(i).getScene_money().isEmpty() && !"null".equals(initOrders.get(i).getScene_money())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getScene_money());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "现付费用不规范,";
                }
                try {
                    if (initOrders.get(i).getCollection() != null && !initOrders.get(i).getCollection().isEmpty() && !"null".equals(initOrders.get(i).getCollection())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getCollection());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "代收货款不规范,";
                } finally {
                    initOrders.get(i).setStill(initOrders.get(i).getCollection());
                }
                ;
                try {
                    if (initOrders.get(i).getExtra_money() != null && !initOrders.get(i).getExtra_money().isEmpty() && !"null".equals(initOrders.get(i).getExtra_money())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getExtra_money());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "落地费不规范,";
                }
                ;
                try {
                    if (initOrders.get(i).getBackAmount() != null && !initOrders.get(i).getBackAmount().isEmpty() && !"null".equals(initOrders.get(i).getBackAmount())) {
                        Double dCheckValue = Double.parseDouble(initOrders.get(i).getBackAmount());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "返款结算不规范,";
                }
                if (!(initOrders.get(i).getSettlement() == null) && !initOrders.get(i).getSettlement().isEmpty() && !"null".equals(initOrders.get(i).getSettlement())) {
                    if (!initOrders.get(i).getSettlement().contains("清账") && !initOrders.get(i).getSettlement().contains("结算")) {
                        normal = 0;
                        result += "只能填写、已清账、待结算,";
                    }
                }
//                if (initOrders.get(i).getConsignee() == null || initOrders.get(i).getConsignee().isEmpty() || initOrders.get(i).getConsignee().isEmpty() || "null".equals(initOrders.get(i).getConsignee())) {
//                    normal = 0;
//                    result += "收货人不能为空,";
//                }
                ;
//                if (initOrders.get(i).getReceivingPhone() == null || initOrders.get(i).getReceivingPhone().isEmpty() || "null".equals(initOrders.get(i).getReceivingPhone())) {
//                    normal = 0;
//                    result += "联系方式不能为空,";
//                }
                ;
                if (initOrders.get(i).getMode() == null || initOrders.get(i).getMode().isEmpty() || "null".equals(initOrders.get(i).getMode())) {
                    normal = 0;
                    result += "交货方式不能为空,";
                }
                //送货也就是配送
                if (initOrders.get(i).getMode() != null && !initOrders.get(i).getMode().contains("自提") && !initOrders.get(i).getMode().contains("配送")&& !initOrders.get(i).getMode().contains("送货")) {
                    normal = 0;
                    result += "交货方式只能填写 自提或者配送,";
                }


                if ("null".equals(initOrders.get(i).getNotice())) {
                    initOrders.get(i).setNotice("");
                }
                if (initOrders.get(i).getNotice() != null && !initOrders.get(i).getNotice().isEmpty() && !initOrders.get(i).getNotice().contains("是") && !initOrders.get(i).getNotice().contains("否")) {
                    normal = 0;
                    result += "等通知发货只能填写 是或者否,";
                }
                if ("null".equals(initOrders.get(i).getReceipt())) {
                    initOrders.get(i).setReceipt("");
                }
                try {
                    if (initOrders.get(i).getArea() == null || initOrders.get(i).getArea().isEmpty() || "null".equals(initOrders.get(i).getArea())) {
                        normal = 0;
                        result += "收货地市/区县不能为空,";
                    }else{
                        Map map=iRegionService.selectReglionId(initOrders.get(i).getArea().trim());
                        if(map!=null){
                            String str[]=map.get("reg").toString().split(",");
                            if(str[2].equals("2")){
                                initOrders.get(i).setCityCode(Integer.valueOf(str[0]));
                                initOrders.get(i).setProvinceCode(Integer.valueOf(str[1]));
                                initOrders.get(i).setAreaCode(Integer.valueOf(str[3]));
                            }else{
                                initOrders.get(i).setProvinceCode(Integer.valueOf(str[3]));
                                initOrders.get(i).setAreaCode(Integer.valueOf(str[0]));
                                initOrders.get(i).setCityCode(Integer.valueOf(str[1]));
                            }
                        }else{
                            normal = 0;
                            result += "地市/区县信息未匹配,";
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    normal = 0;
                    result += "地市/区县信息未匹配,";
                }
                initOrders.get(i).setDotId(ShiroKit.getUser().getDotId());
                initOrders.get(i).setDeptId(ShiroKit.getUser().getDeptId());
                initOrders.get(i).setBatchNo(bath_no);
                String mode = initOrders.get(i).getMode() == null ? "" : initOrders.get(i).getMode();
                initOrders.get(i).setOrderNum((mode.contains("自提") ? "ZT" : "PS") + bath_no + i);
                initOrders.get(i).setUserId(ShiroKit.getUser().getId() + "");
                initOrders.get(i).setIsNormal(normal + "");
                initOrders.get(i).setResult(result);
            }
            //获取配送范围
            DeliveryRange deliveryRange = iDeliveryRangeService.selectByDotId(ShiroKit.getUser().getDotId());
            String[] deliver = null;
            if (deliveryRange != null) {
                deliver = deliveryRange.getRegPlaceName().split(",");
            }
            int zt_count = 0, zz_count = 0, ps_count = 0;

            List<TOrder> list_order = new ArrayList<TOrder>();
            for (int i = 0; i < initOrders.size(); i++) {
                if ("1".equals(initOrders.get(i).getIsNormal())) {
                    if (deliver != null) {
                        boolean flag = false;
                        for (String s : deliver) {
                            if (s.equals(initOrders.get(i).getAreaCode() + "")) {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag) {
                            zz_count++;
                            initOrders.get(i).setIsTransfer("1");
                        } else {
                            initOrders.get(i).setIsTransfer("0");
                        }
                    } else {
                        initOrders.get(i).setIsTransfer("0");//0 否 1是 中转单
                    }
                    TOrder torder = new TOrder();
                    BeanUtils.copyProperties(initOrders.get(i), torder);
                    if (initOrders.get(i).getGoodsPiece() != null) {
                        torder.setGoodsPiece(Integer.parseInt(initOrders.get(i).getGoodsPiece().split("\\.")[0]));
                    }
                    if (initOrders.get(i).getGoodsVolume() != null) {
                        torder.setGoodsVolume(Double.parseDouble(initOrders.get(i).getGoodsVolume()));
                    }
                    if (initOrders.get(i).getGoodsWeight() != null) {
                        torder.setGoodsWeight(Double.parseDouble(initOrders.get(i).getGoodsWeight()));
                    }
                    if (initOrders.get(i).getBackAmount() != null) {
                        torder.setBackAmount(Double.parseDouble(initOrders.get(i).getBackAmount()));
                    }
                    if (initOrders.get(i).getStill() != null) {
                        torder.setStill(Double.parseDouble(initOrders.get(i).getStill()));
                    }
                    if (initOrders.get(i).getCollection() != null) {
                        torder.setCollection(Double.parseDouble(initOrders.get(i).getCollection()));
                    }
                    if (initOrders.get(i).getPayment() != null) {
                        torder.setPayment(Double.parseDouble(initOrders.get(i).getPayment()));
                    }
                    if (initOrders.get(i).getExtra_money() != null) {
                        torder.setExtra_money(Double.parseDouble(initOrders.get(i).getExtra_money()));
                    }
                    torder.setProvince_code(initOrders.get(i).getProvinceCode());
                    torder.setCity_code(initOrders.get(i).getCityCode());
                    torder.setArea_code(initOrders.get(i).getAreaCode());
                    torder.setUserId(Integer.parseInt(initOrders.get(i).getUserId()));
                    if (initOrders.get(i).getMode() != null && initOrders.get(i).getMode().contains("自提")) {
                        if (initOrders.get(i).getIsTransfer().equals("0")) {//非中转单才需要记为自提
                            zt_count++;
                        }
                        torder.setMode(0);
                    } else if (initOrders.get(i).getMode() != null && (initOrders.get(i).getMode().contains("配送")||initOrders.get(i).getMode().contains("送货"))) {
                        if (initOrders.get(i).getIsTransfer().equals("0")) {//非中转单才需要记为配送
                            ps_count++;
                        }
                        torder.setMode(1);

                    }
                    if (initOrders.get(i).getSettlement() != null && initOrders.get(i).getSettlement().contains("清账")) {
                        torder.setSettlement(0);
                    } else if (initOrders.get(i).getSettlement() != null && initOrders.get(i).getSettlement().contains("结算")) {
                        torder.setSettlement(1);
                    }
                    if (initOrders.get(i).getNotice() != null && initOrders.get(i).getNotice().contains("是")) {
                        torder.setNotice("1");
                    } else if (initOrders.get(i).getNotice() != null && initOrders.get(i).getNotice().contains("否")) {
                        torder.setNotice("0");
                    }
                    if(initOrders.get(i).getReceipt()==null||initOrders.get(i).getReceipt().isEmpty()){
                        torder.setReceiptStatus(3);//若为空则无需回单
                    }
                    if (initOrders.get(i).getNotice() != null && initOrders.get(i).getNotice().contains("是")) {
                        torder.setNotice("1");
                    } else if (initOrders.get(i).getNotice() != null && initOrders.get(i).getNotice().contains("否")) {
                        torder.setNotice("0");
                    }
                    torder.setDriverId(Integer.valueOf(driverId));
                    torder.setOrder_date(initOrders.get(i).getOrder_date());
                    torder.setStatus(1);//订单状态 0-审核中 1-待出库 2-已出库 3-完成
                    list_order.add(torder);
                }
            }

            boolean ret = this.insertBatch(initOrders);//插入原始表
            if (list_order.size()==exportOrders.size()) {
                //插入运单表
                TTrain tTrain=new TTrain();
                tTrain.setCarNo(car_no);
                tTrain.setDot_id(ShiroKit.getUser().getDotId());
                tTrain.setDriverName(driver_name);
                tTrain.setPhone(phone);
                tTrain.setSendDeptName(tCooperationDot.getDotName());
                tTrain.setTrainNo(bath_no);
                tTrain.setTrain_type("0");//0 初始 1 自提 2配送 3中转
                tTrain.setSendDeptId(transferId);
                tTrain.setUserId(ShiroKit.getUser().getId());
                tTrain.setCreateTime(new Date());
                tTrain.setDriver_id(driverId);
                tTrain.setReportDate(DateUtil.parseDate(arrive_date));
                tTrainMapper.insert(tTrain);
                //订单全部正常 才导入订单表
                for (TOrder tOrder : list_order) {
                    tOrder.setStatus(0);//订单状态 0-审核中 1-待出库 2-已出库 3-完成
                    tOrder.setBacth_id(tTrain.getId());
                    boolean flag = itOrderService.insert(tOrder);
                    if (flag) {
                        itOrderService.checkOrder(tOrder, ShiroKit.getUser().getDeptId(), tOrder.getDotId() + "", ShiroKit.getUser().getId());
                    }
                }


            }
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("ZT", zt_count);
            m.put("PS", ps_count);
            m.put("ZZ", zz_count);
            m.put("BATCH_NO", bath_no);
            m.put("SUCCESS", list_order.size());
            m.put("FAILUER", exportOrders.size() - list_order.size());
            return m;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }

    }
}
